<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>页面编辑</title>
    {% include 'system/common/header.html' %}
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">编号</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ page.id }}" name="id" lay-verify="title"
                               autocomplete="off" placeholder="请输入标题" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">书籍</label>
                    <div class="layui-input-block">
                        <ul id="selectParent" name="book_id" class="dtree" data-id="0"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ page.title }}" name="title" lay-verify="title"
                               autocomplete="off" placeholder="请输入标题" class="layui-input">
                    </div>
                </div>
                 <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ page.sort }}" name="sort" lay-verify="sort"
                               autocomplete="off" placeholder="请输入页面排序" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">内容</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入描述" name="content" class="layui-textarea">{{ page.content }}</textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="page-update">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'system/common/footer.html' %}
<script>
    layui.use(['form', 'jquery', 'dtree'], function () {
        let form = layui.form
        let $ = layui.jquery
        let dtree = layui.dtree

        dtree.renderSelect({
            elem: '#selectParent',
            url: '/zbook/book/tree',
            method: 'get',
            selectInputName: {nodeId: 'bookId', context: 'title'},
            skin: 'layui',
            dataFormat: 'list',
            response: {treeId: 'id', parentId: 'parent_id', title: 'name'},
            selectInitVal: "{{ page.book_id }}"
        })
        form.on('submit(page-update)', function (data) {
            let roleIds = ''
            $('input[type=checkbox]:checked').each(function () {
                roleIds += $(this).val() + ','
            })
            roleIds = roleIds.substr(0, roleIds.length - 1)
            data.field.roleIds = roleIds
            $.ajax({
                url: '/zbook/page/update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
                            parent.layui.table.reload('page-table')
                        })
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000})
                    }
                }
            })
            return false
        })
    })
</script>
</body>
</html>